import axios from "axios"
import { BASE_URL, TIMEOUT } from "./config"

class api {
  constructor(baseURL, timeout) {
    this.instance = axios.create({
      baseURL,
      timeout
    })

    // 响应拦截器
    this.instance.interceptors.response.use((res) => {
      return res.data
    }, err => {
      console.log(err);
      return null
    })

    // 请求拦截器
    this.instance.interceptors.request.use((config) => {
      return config
    }, err => {
      return err
    })
  }

  request(config) {
    return this.instance.request(config)
  }

  get(config) {
    return this.request({...config, method: 'get'})
  }

  post(config) {
    return this.request({...config, method: 'post'})
  }
}

export default new api(BASE_URL, TIMEOUT)